import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  // 定义状态
  state: {
    field: '', // 信息类型
    defaultField: 'recommend',
    navInfo: { // 菜单信息，跟field对应
      'recommend': {key: 0, name: '推荐'},
      'movie': {key: 1, name: '电影'},
      'tel': {key: 2, name: '剧集'},
      'actor': {key: 3, name: '明星'},
      'variety': {key: 4, name: '综艺'},
      'fashion': {key: 5, name: '时尚'},
      'music': {key: 6, name: '音乐'},
    }
  },
  mutations: {
    SETFIELD (state, field) {
      state.field = field;
    }
  },
  modules : {

  },
  getters: {
    getNavByKey: (state) => (key) => {
      let keyNum = Number.parseInt(key);
      let navInfo = state.navInfo;
      for (let code in navInfo) {
        let item = navInfo[code];
        if (item.key === keyNum) {
          return {code, name: item.name, key: item.key}
        }
      }
      return null;
    },
    navList: (state) => {
      let navInfo = state.navInfo;
      let result = [];
      for (let code in navInfo) {
        let item = navInfo[code];
        result.push({code, name: item.name, key: item.key})
      }
      return result;
    }
  }

});


export default store
